#####################################################################################
####Name of File: anc_comparison_statevnatl.R
####Input dataset: "Unrestricted_SWU.Rdata"
####Output : state specific regression output
####Date: 4/24/2011
####Purpose: regress anc on jsy and covariates for each state, and create ROC plots
#####################################################################################


###NOTE: MUST FIRST RUN THE FOLLOWING FILES TO MAKE THIS WORK:
##"National_outcome_reg.R" and "anc_statespecific.R"


###take national model betas- anc.logit

summary(anc.logit)


###do state for uttar pradesh
DF1<-as.data.frame(all.data2[[32,1]])

DF1$nb_cat<-as.factor(DF1$nb_cat_num)
DF1$mateduc_cat<-as.factor(DF1$mateduc_cat)
DF1$dlhs3_country_decile<-as.factor(DF1$dlhs3_country_decile)
DF1$caste1<-as.factor(DF1$caste)
DF1$state1<-as.factor(DF1$state)
DF1$dist.char<-as.character(DF1$DIST_mean_hh_p_income3)
DF1$district<-as.factor(DF1$dist.char)
DF1$religion2<-as.factor(DF1$religion.char)

print(unique(DF1$state1))


anc.logit1<-zelig(anc3~jsy+matage_cat+nb_cat+birth_interval+mult_birth+mateduc_cat+dlhs3_country_decile+caste1+ religion2+res_cat+ 
+district, model="logit", weights="weights", data=DF1, save.data=T)

summary(anc.logit1)



####do state for Rajasthan

DF1<-as.data.frame(all.data2[[28,1]])

DF1$nb_cat<-as.factor(DF1$nb_cat_num)
DF1$mateduc_cat<-as.factor(DF1$mateduc_cat)
DF1$dlhs3_country_decile<-as.factor(DF1$dlhs3_country_decile)
DF1$caste1<-as.factor(DF1$caste)
DF1$state1<-as.factor(DF1$state)
DF1$dist.char<-as.character(DF1$DIST_mean_hh_p_income3)
DF1$district<-as.factor(DF1$dist.char)
DF1$religion2<-as.factor(DF1$religion.char)

print(unique(DF1$state1))


anc.logit.raj<-zelig(anc3~jsy+matage_cat+nb_cat+birth_interval+mult_birth+mateduc_cat+dlhs3_country_decile+caste1+ religion2+res_cat+ 
+district, model="logit", weights="weights", data=DF1, save.data=T)

summary(anc.logit1)


####do state for Madhya Pradesh

DF1<-as.data.frame(all.data2[[20,1]])

DF1$nb_cat<-as.factor(DF1$nb_cat_num)
DF1$mateduc_cat<-as.factor(DF1$mateduc_cat)
DF1$dlhs3_country_decile<-as.factor(DF1$dlhs3_country_decile)
DF1$caste1<-as.factor(DF1$caste)
DF1$state1<-as.factor(DF1$state)
DF1$dist.char<-as.character(DF1$DIST_mean_hh_p_income3)
DF1$district<-as.factor(DF1$dist.char)
DF1$religion2<-as.factor(DF1$religion.char)

print(unique(DF1$state1))


anc.logit.MP<-zelig(anc3~jsy+matage_cat+nb_cat+birth_interval+mult_birth+mateduc_cat+dlhs3_country_decile+caste1+ religion2+res_cat+ 
+district, model="logit", weights="weights", data=DF1, save.data=T)



####do state for Bihar

DF1<-as.data.frame(all.data2[[5,1]])

DF1$nb_cat<-as.factor(DF1$nb_cat_num)
DF1$mateduc_cat<-as.factor(DF1$mateduc_cat)
DF1$dlhs3_country_decile<-as.factor(DF1$dlhs3_country_decile)
DF1$caste1<-as.factor(DF1$caste)
DF1$state1<-as.factor(DF1$state)
DF1$dist.char<-as.character(DF1$DIST_mean_hh_p_income3)
DF1$district<-as.factor(DF1$dist.char)
DF1$religion2<-as.factor(DF1$religion.char)

print(unique(DF1$state1))


anc.logit.Bihar<-zelig(anc3~jsy+matage_cat+nb_cat+birth_interval+mult_birth+mateduc_cat+dlhs3_country_decile+caste1+ religion2+res_cat+ 
+district, model="logit", weights="weights", data=DF1, save.data=T)

####do state for Sikkim

DF1<-as.data.frame(all.data2[[29,1]])

DF1$nb_cat<-as.factor(DF1$nb_cat_num)
DF1$mateduc_cat<-as.factor(DF1$mateduc_cat)
DF1$dlhs3_country_decile<-as.factor(DF1$dlhs3_country_decile)
DF1$caste1<-as.factor(DF1$caste)
DF1$state1<-as.factor(DF1$state)
DF1$dist.char<-as.character(DF1$DIST_mean_hh_p_income3)
DF1$district<-as.factor(DF1$dist.char)
DF1$religion2<-as.factor(DF1$religion.char)

print(unique(DF1$state1))


anc.logit.Sikkim<-zelig(anc3~jsy+matage_cat+nb_cat+birth_interval+mult_birth+mateduc_cat+dlhs3_country_decile+caste1+ religion2+res_cat+ 
+district, model="logit", weights="weights", data=DF1, save.data=T)


####do state for Sikkim

DF1<-as.data.frame(all.data2[[29,1]])

DF1$nb_cat<-as.factor(DF1$nb_cat_num)
DF1$mateduc_cat<-as.factor(DF1$mateduc_cat)
DF1$dlhs3_country_decile<-as.factor(DF1$dlhs3_country_decile)
DF1$caste1<-as.factor(DF1$caste)
DF1$state1<-as.factor(DF1$state)
DF1$dist.char<-as.character(DF1$DIST_mean_hh_p_income3)
DF1$district<-as.factor(DF1$dist.char)
DF1$religion2<-as.factor(DF1$religion.char)

print(unique(DF1$state1))


anc.logit.Sikkim<-zelig(anc3~jsy+matage_cat+nb_cat+birth_interval+mult_birth+mateduc_cat+dlhs3_country_decile+caste1+ religion2+res_cat+ 
+district, model="logit", weights="weights", data=DF1, save.data=T)


####do state for Karnataka

DF1<-as.data.frame(all.data2[[17,1]])

DF1$nb_cat<-as.factor(DF1$nb_cat_num)
DF1$mateduc_cat<-as.factor(DF1$mateduc_cat)
DF1$dlhs3_country_decile<-as.factor(DF1$dlhs3_country_decile)
DF1$caste1<-as.factor(DF1$caste)
DF1$state1<-as.factor(DF1$state)
DF1$dist.char<-as.character(DF1$DIST_mean_hh_p_income3)
DF1$district<-as.factor(DF1$dist.char)
DF1$religion2<-as.factor(DF1$religion.char)

print(unique(DF1$state1))


anc.logit.Karnataka<-zelig(anc3~jsy+matage_cat+nb_cat+birth_interval+mult_birth+mateduc_cat+dlhs3_country_decile+caste1+ religion2+res_cat+ 
+district, model="logit", weights="weights", data=DF1, save.data=T)






#######################################ROC curves

par(mfrow=c(3,2))



#########Bihar



###fitted values
Bihar.fitted.natl<-fitted(anc.logit)[full.data$state=="Bihar"]
Bihar.fitted.state<-fitted(anc.logit.Bihar)

length(Bihar.fitted.natl)
length(fitted(anc.logit.Bihar))

###y values
Bihar.response.natl<-anc.logit$y[full.data$state=="Bihar"]
length(Bihar.response.natl)

Bihar.response.state<-anc.logit.Bihar$y
length(Bihar.response.state)

###ROC plot comparing state specific for state of Uttar Pradesh
rocplot(Bihar.response.state ,Bihar.response.natl,  Bihar.fitted.state,Bihar.fitted.natl,  main="Bihar (High Focus)")




##############Uttar Pradesh
###fitted values
UP.fitted.natl<-fitted(anc.logit)[full.data$state=="Uttar Pradesh"]
UP.fitted.state<-fitted(anc.logit1)

length(UP.fitted.natl)
length(fitted(anc.logit1))

###y values
UP.response.natl<-anc.logit$y[full.data$state=="Uttar Pradesh"]
length(UP.response.natl)

UP.response.state<-anc.logit1$y
length(UP.response.state)

###ROC plot comparing state specific for state of Uttar Pradesh
rocplot(UP.response.state , UP.response.natl, UP.fitted.state,UP.fitted.natl,main="Uttar Pradesh (High Focus)")





#########Madhya Pradesh



###fitted values
MP.fitted.natl<-fitted(anc.logit)[full.data$state=="Madhya Pradesh"]
MP.fitted.state<-fitted(anc.logit.MP)

length(MP.fitted.natl)
length(fitted(anc.logit.MP))

###y values
MP.response.natl<-anc.logit$y[full.data$state=="Madhya Pradesh"]
length(MP.response.natl)

MP.response.state<-anc.logit.MP$y
length(MP.response.state)

###ROC plot comparing state specific for state of Uttar Pradesh
rocplot(MP.response.state ,MP.response.natl,  MP.fitted.state,MP.fitted.natl,  main="Madhya Pradesh (High Focus)")





#########Karnataka



###fitted values
Karnataka.fitted.natl<-fitted(anc.logit)[full.data$state=="Karnataka"]
Karnataka.fitted.state<-fitted(anc.logit.Karnataka)

length(Karnataka.fitted.natl)
length(fitted(anc.logit.Karnataka))

###y values
Karnataka.response.natl<-anc.logit$y[full.data$state=="Karnataka"]
length(Karnataka.response.natl)

Karnataka.response.state<-anc.logit.Karnataka$y
length(Karnataka.response.state)

###ROC plot comparing state specific for state of Uttar Pradesh
rocplot(Karnataka.response.state ,Karnataka.response.natl,  Karnataka.fitted.state,Karnataka.fitted.natl,  main="Karnataka")


##########Rajasthan


###fitted values
Raj.fitted.natl<-fitted(anc.logit)[full.data$state=="Rajasthan"]
Raj.fitted.state<-fitted(anc.logit.raj)

length(Raj.fitted.natl)
length(fitted(anc.logit.raj))

###y values
Raj.response.natl<-anc.logit$y[full.data$state=="Rajasthan"]
length(Raj.response.natl)

Raj.response.state<-anc.logit.raj$y
length(Raj.response.state)

###ROC plot comparing state specific for state of Uttar Pradesh
rocplot(Raj.response.state ,Raj.response.natl,Raj.fitted.state, Raj.fitted.natl, main="Rajasthan (High Focus)")


#########Sikkim


###fitted values
Sikkim.fitted.natl<-fitted(anc.logit)[full.data$state=="Sikkim"]
Sikkim.fitted.state<-fitted(anc.logit.Sikkim)

length(Sikkim.fitted.natl)
length(fitted(anc.logit.Sikkim))

###y values
Sikkim.response.natl<-anc.logit$y[full.data$state=="Sikkim"]
length(Sikkim.response.natl)

Sikkim.response.state<-anc.logit.Sikkim$y
length(Sikkim.response.state)

###ROC plot comparing state specific for state of Uttar Pradesh
rocplot(Sikkim.response.state ,Sikkim.response.natl,  Sikkim.fitted.state,Sikkim.fitted.natl,  main="Sikkim")





















